import { ReceiptType, SupplierItem, WarehouseItem, ReceiptItem, ProductFileItem } from "./models";
import req from "/@/api/req";

export const Api = {
  supplierPage: "mall-ums/api/v1/pis/suppliers/page",
  suppliers: "mall-ums/api/v1/pis/suppliers",
  supplierList: "mall-ums/api/v1/pis/suppliers/list",

  addressBookPage: "mall-ums/api/v1/pis/address-books/page",
  addressBooks: "mall-ums/api/v1/pis/address-books",

  idLicencePage: "mall-ums/api/v1/pis/id-licences/page",
  idLicences: "mall-ums/api/v1/pis/id-licences",

  warehouses: "mall-ums/api/v1/pis/warehouses",
  warehouseRoutes: "mall-ums/api/v1/pis/warehouses/routes",
  warehouseList: "mall-ums/api/v1/pis/warehouses/list",

  trainNumbers: "mall-ums/api/v1/pis/train-numbers",
  trainNumberPage: "mall-ums/api/v1/pis/train-numbers/page",

  productFiles: "mall-ums/api/v1/pis/product-files",
  productFilePage: "mall-ums/api/v1/pis/product-files/page",
  productIntroduction: "mall-ums/api/v1/pis/product-files/introduction",

  warehousingReceiptPage: "mall-ums/api/v1/pis/goods-warehousing/page",
  warehousingReceipt: "mall-ums/api/v1/pis/goods-warehousing",
  warehousingReceiptDetail: "mall-ums/api/v1/pis/goods-warehousing/{id}/form",
  warehousingReceiptReview: "mall-ums/api/v1/pis/goods-warehousing/review",
  warehousingReceiptNullify: "mall-ums/api/v1/pis/goods-warehousing/nullify",

  deliveryReceiptPage: "mall-ums/api/v1/pis/goods-deliveries/page",
  deliveryReceipt: "mall-ums/api/v1/pis/goods-deliveries",
  deliveryReceiptDetail: "mall-ums/api/v1/pis/goods-deliveries/{id}/form",
  deliveryReceiptReview: "mall-ums/api/v1/pis/goods-deliveries/review",
  deliveryReceiptNullify: "mall-ums/api/v1/pis/goods-deliveries/nullify",

  returnReceiptPage: "mall-ums/api/v1/pis/goods-returns/page",
  returnReceipt: "mall-ums/api/v1/pis/goods-returns",
  returnReceiptDetail: "mall-ums/api/v1/pis/goods-returns/{id}/form",
  returnReceiptReview: "mall-ums/api/v1/pis/goods-returns/review",
  returnReceiptNullify: "mall-ums/api/v1/pis/goods-returns/nullify",

  adjustmentReceiptPage: "mall-ums/api/v1/pis/goods-adjustments/page",
  adjustmentReceipt: "mall-ums/api/v1/pis/goods-adjustments",
  adjustmentReceiptDetail: "mall-ums/api/v1/pis/goods-adjustments/{id}/form",
  adjustmentReceiptReview: "mall-ums/api/v1/pis/goods-adjustments/review",
  adjustmentReceiptNullify: "mall-ums/api/v1/pis/goods-adjustments/nullify",

  profitLossReceiptPage: "mall-ums/api/v1/pis/goods-profit-losses/page",
  profitLossReceiptDetail: "mall-ums/api/v1/pis/goods-profit-losses/{id}/form",
  profitLossReceiptReview: "mall-ums/api/v1/pis/goods-profit-losses/review",
  profitLossReceipt: "mall-ums/api/v1/pis/goods-profit-losses",
  profitLossReceiptNullify: "mall-ums/api/v1/pis/goods-profit-losses/nullify",

  purchaseReceiptPage: "mall-ums/api/v1/pis/purchase-orders/page",
  purchaseReceiptDetail: "mall-ums/api/v1/pis/purchase-orders/{id}/form",
  purchaseReceiptReview: "mall-ums/api/v1/pis/purchase-orders/review",
  purchaseReceipt: "mall-ums/api/v1/pis/purchase-orders",
  purchaseReceiptNullify: "mall-ums/api/v1/pis/purchase-orders/nullify",

  sellReceiptPage: "mall-ums/api/v1/pis/goods-sale/page",
  sellReceiptDetail: "mall-ums/api/v1/pis/goods-sale/{id}/form",
  sellReceiptReview: "mall-ums/api/v1/pis/goods-sale/review",
  sellReceipt: "mall-ums/api/v1/pis/goods-sale",
  sellReceiptNullify: "mall-ums/api/v1/pis/goods-sale/nullify",

  goodsBatch: "mall-ums/api/v1/pis/goods-batch-inventories",
};

export function getSupplierList() {
  return req.commonGet<SupplierItem[]>(Api.supplierList, {});
}

export function getWarehouseList() {
  return req.commonGet<WarehouseItem[]>(Api.warehouseList, {});
}

export function addWarehousingReceipt(data: Partial<ReceiptItem>, receiptType: ReceiptType) {
  return req.commonPost(Api[receiptType + "Receipt"], data, { useGlobalLoading: true });
}

export function delWarehousingReceipt(id: string, receiptType: ReceiptType) {
  return req.delete(
    {
      url: Api[receiptType + "Receipt"] + "/" + id,
    },
    {
      useGlobalLoading: true,
    }
  );
}

export function updateWarehousingReceipt(data: Partial<ReceiptItem>, receiptType: ReceiptType) {
  return req.put(
    {
      url: Api[receiptType + "Receipt"],
      data,
    },
    {
      useGlobalLoading: true,
    }
  );
}
export function getWarehousingReceipt(id: number, receiptType: ReceiptType) {
  return req.commonGet<ReceiptItem>(
    Api[receiptType + "ReceiptDetail"].replace("{id}", id + ""),
    {},
    { useGlobalLoading: true }
  );
}

export function wrReview(id: number, receiptType: ReceiptType) {
  return req.put(
    {
      url: Api[receiptType + "ReceiptReview"],
      data: { id },
    },
    { useGlobalLoading: true }
  );
}

export function wrNullify(id: number, receiptType: ReceiptType) {
  return req.put(
    {
      url: Api[receiptType + "ReceiptNullify"],
      data: { id },
    },
    { useGlobalLoading: true }
  );
}

export function updateIntroduction(id: number, introduction: string) {
  return req.put(
    {
      url: Api.productIntroduction,
      data: { id, introduction },
    },
    { useGlobalLoading: true }
  );
}

export function updateProductRatio(productId: number, ratio: number) {
  return req.put(
    {
      url: Api.productFiles,
      data: { id: productId, pointRedemptionRatio: ratio },
    },
    { useGlobalLoading: true }
  );
}

export function getGoodsBatch(productId: number, warehouseId: string) {
  return req.commonGet<ProductFileItem[]>(Api.goodsBatch, { productId, warehouseId }, { useGlobalLoading: false });
}
